package com.bls.sms.bo.impl;

import java.util.List;

import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;

import com.bls.sms.bo.interfaces.SubjectBO;
import com.bls.sms.dao.interfaces.SubjectDAO;
import com.bls.sms.exception.ApplicationException;
import com.bls.sms.wrapper.Subject;

public class SubjectBOImpl extends BaseBO implements SubjectBO {
	private SubjectDAO subjectDAO = null;

	public int saveSubject(Subject subject) throws ApplicationException {
		final Subject subj = subject;
		getTransactionTemplate().execute(new TransactionCallback() {
			public Object doInTransaction(TransactionStatus status) {
				try {
					try {
						// int maxId = getSubjectDAO().getMaxId() + 1;
						subj.setId(new Integer(2));
						getSubjectDAO().saveSubject(subj);
					} catch (ApplicationException e) {
						e.printStackTrace();
					}
				} finally {
					status.setRollbackOnly();
				}
				return null;
			}
		});
		return 1;
	}

	public int updateSubject(Subject subject) throws ApplicationException {
		final Subject subj = subject;
		getTransactionTemplate().execute(new TransactionCallback() {
			public Object doInTransaction(TransactionStatus status) {
				try {
					try {
						getSubjectDAO().updateSubject(subj);
					} catch (ApplicationException e) {
						e.printStackTrace();
					}
				} finally {
					status.setRollbackOnly();
				}
				return null;
			}
		});
		return 1;
	}

	public int deleteSubject(int subjectId) throws ApplicationException {
		final int subj = subjectId;
		getTransactionTemplate().execute(new TransactionCallback() {
			public Object doInTransaction(TransactionStatus status) {
				try {
					try {
						getSubjectDAO().deleteSubject(subj);
					} catch (ApplicationException e) {
						e.printStackTrace();
					}
				} finally {
					status.setRollbackOnly();
				}
				return null;
			}
		});
		return 1;
	}

	public List<Subject> loadSubjects() throws ApplicationException {
		return getSubjectDAO().loadSubjects();
	}

	public Subject getSubjectDetails(int subjectId) throws ApplicationException {
		return getSubjectDAO().getSubjectDetails(subjectId);
	}

	public SubjectDAO getSubjectDAO() {
		return subjectDAO;
	}

	public void setSubjectDAO(SubjectDAO subjectDAO) {
		this.subjectDAO = subjectDAO;
	}
}
